package Cryptographic;

import java.security.NoSuchAlgorithmException;

import Verifier.GeneralUtil;

/* This class test the Cryptosystem */
public class Test {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception 
	{
		/* create HashFunction objects */
		HashFunction sha256 = new HashFunction("SHA-256");
		HashFunction sha384 = new HashFunction("SHA-384");
		HashFunction sha512 = new HashFunction("SHA-512"); 
		
		/* create PRG objects */
		PRG prg256 = new PRG(sha256);
		PRG prg384 = new PRG(sha384); 
		PRG prg512 = new PRG(sha512); 
		
		/* create RandomOracle objects */
		RandomOracle RO256_65 = new RandomOracle(sha256, 65);
		RandomOracle RO256_261 = new RandomOracle(sha256, 261);
		RandomOracle RO384_93 = new RandomOracle(sha384, 93);
		RandomOracle RO384_411 = new RandomOracle(sha384, 411);
		RandomOracle RO512_111 = new RandomOracle(sha512, 111);
		RandomOracle RO512_579 = new RandomOracle(sha512, 579);
		
		/* seeds and their expected output */
		String seed1 = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
		String expectedOutput1 = "70f4003d52b6eb03da852e93256b5986b5d4883098bb7973bc5318cc66637a84" +
								 "04a6950a06d3e3308ad7d3606ef810eb124e3943404ca746a12c51c7bf776839" +
								 "0f8d842ac9cb62349779a7537a78327d545aaeb33b2d42c7d1dc3680a4b23628" +
								 "627e9db8ad47bfe76dbe653d03d2c0a35999ed28a5023924150d72508668d244";
		
		String seed2 = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" +
					   "202122232425262728292a2b2c2d2e2f";
		String expectedOutput2 = "e45ac6c0cafff343b268d4cbd773328413672a764df99ab823b53074d94152bd" +
								 "27fc38bcffdb7c1dc1b6a3656b2d4819352c482da40aad3b37f333c7afa81a92" +
								 "b7b54551f3009efa4bdb8937492c5afca1b141c99159b4f0f819977a4e10eb51" +
								 "61edd4b1734717de4106f9c184a17a9b5ee61a4399dd755f322f5d707a581cc1";
		
		String seed3 = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" +
					   "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f";
		String expectedOutput3 = "979043771043f4f8e0a2a19b1fbfbe5a8f076c2b5ac003e0b9619e0c45faf767" +
								 "47295734980602ec1d8d3cd249c165b7db62c976cb9075e35d94197c0f06e1f3" +
								 "97a45017c508401d375ad0fa856da3dfed20847716755c6b03163aec2d9f43eb" +
								 "c2904f6e2cf60d3b7637f656145a2d32a6029fbda96361e1b8090c9712a48938";
		
		String seed4 = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
		String expectedOutput4 = "001a8d6b6f65899ba5";
		
		String seed5 = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
		String expectedOutput5 = "1c04f57d5f5856824bca3af0ca466e283593bfc556ae2e9f4829c7ba8eb76db878";
		
		String seed6 = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
		String expectedOutput6 = "04713a5e22935833d436d1db";
		
		String seed7 = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
		String expectedOutput7 = "00dc086c320e38b92722a9c0f87f2f5de81b976400e2441da542d1c3f3f391e4" +
								 "1d6bcd8297c541c2431a7272491f496b622266aa";
		
		String seed8 = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
		String expectedOutput8 = "28d742c34b97367eb968a3f28b6c";
		
		String seed9 = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
		String expectedOutput9 = "00a6f79b8450fef79af71005c0b1028c9f025f322f1485c2b245f658fe641d47" +
								 "dcbb4fe829e030b52e4a81ca35466ad1ca9be6feccb451e7289af318ddc9dae0" +
								 "98a5475d6119ff6fe0";
		
		
		/* compute values */
		byte[] ret1 = prg256.compute(GeneralUtil.hexStringToByteArray(seed1), 0, 127);
		byte[] ret2 = prg384.compute(GeneralUtil.hexStringToByteArray(seed2), 0, 127);
		byte[] ret3 = prg512.compute(GeneralUtil.hexStringToByteArray(seed3), 0, 127);
		byte[] ret4 = RO256_65.compute(GeneralUtil.hexStringToByteArray(seed4));
		byte[] ret5 = RO256_261.compute(GeneralUtil.hexStringToByteArray(seed5));
		byte[] ret6 = RO384_93.compute(GeneralUtil.hexStringToByteArray(seed6));
		byte[] ret7 = RO384_411.compute(GeneralUtil.hexStringToByteArray(seed7));
		byte[] ret8 = RO512_111.compute(GeneralUtil.hexStringToByteArray(seed8));
		byte[] ret9 = RO512_579.compute(GeneralUtil.hexStringToByteArray(seed9));
		
		/* print outputs and check corectness */
		System.out.println("The output of PRG(Hashfunction(\"SHA-256\")) on the seed " + seed1 + " is:");
		System.out.println(GeneralUtil.byteArrayToHexString(ret1));
		if(expectedOutput1.compareToIgnoreCase(GeneralUtil.byteArrayToHexString(ret1)) == 0)
			System.out.println("and it is equal to the expected output");
		else
			System.out.println("and it is not equal to the expected output");
		
		System.out.println("\n***************************************************\n");
		
		System.out.println("The output of PRG(Hashfunction(\"SHA-384\")) on the seed " + seed2 + " is:");
		System.out.println(GeneralUtil.byteArrayToHexString(ret2));
		if(expectedOutput2.compareToIgnoreCase(GeneralUtil.byteArrayToHexString(ret2)) == 0)
			System.out.println("and it is equal to the expected output");
		else
			System.out.println("and it is not equal to the expected output");
		
		System.out.println("\n***************************************************\n");
		
		System.out.println("The output of PRG(Hashfunction(\"SHA-512\")) on the seed " + seed3 + " is:");
		System.out.println(GeneralUtil.byteArrayToHexString(ret3));
		if(expectedOutput3.compareToIgnoreCase(GeneralUtil.byteArrayToHexString(ret3)) == 0)
			System.out.println("and it is equal to the expected output");
		else
			System.out.println("and it is not equal to the expected output");
		
		System.out.println("\n***************************************************\n");
		
		System.out.println("The output of RandomOracle(Hashfunction(\"SHA-256\"), 65) on the seed " + seed4 + " is:");
		System.out.println(GeneralUtil.byteArrayToHexString(ret4));
		if(expectedOutput4.compareToIgnoreCase(GeneralUtil.byteArrayToHexString(ret4)) == 0)
			System.out.println("and it is equal to the expected output");
		else
			System.out.println("and it is not equal to the expected output");
		
		System.out.println("\n***************************************************\n");
		
		System.out.println("The output of RandomOracle(Hashfunction(\"SHA-256\"), 261) on the seed " + seed5 + " is:");
		System.out.println(GeneralUtil.byteArrayToHexString(ret5));
		if(expectedOutput5.compareToIgnoreCase(GeneralUtil.byteArrayToHexString(ret5)) == 0)
			System.out.println("and it is equal to the expected output");
		else
			System.out.println("and it is not equal to the expected output");
		
		System.out.println("\n***************************************************\n");
		
		System.out.println("The output of RandomOracle(Hashfunction(\"SHA-384\"), 93) on the seed " + seed6 + " is:");
		System.out.println(GeneralUtil.byteArrayToHexString(ret6));
		if(expectedOutput6.compareToIgnoreCase(GeneralUtil.byteArrayToHexString(ret6)) == 0)
			System.out.println("and it is equal to the expected output");
		else
			System.out.println("and it is not equal to the expected output");
		
		System.out.println("\n***************************************************\n");
		
		System.out.println("The output of RandomOracle(Hashfunction(\"SHA-384\"), 411) on the seed " + seed7 + " is:");
		System.out.println(GeneralUtil.byteArrayToHexString(ret7));
		if(expectedOutput7.compareToIgnoreCase(GeneralUtil.byteArrayToHexString(ret7)) == 0)
			System.out.println("and it is equal to the expected output");
		else
			System.out.println("and it is not equal to the expected output");
		
		
		System.out.println("\n***************************************************\n");
		
		System.out.println("The output of RandomOracle(Hashfunction(\"SHA-512\"), 111) on the seed " + seed8 + " is:");
		System.out.println(GeneralUtil.byteArrayToHexString(ret8));
		if(expectedOutput8.compareToIgnoreCase(GeneralUtil.byteArrayToHexString(ret8)) == 0)
			System.out.println("and it is equal to the expected output");
		else
			System.out.println("and it is not equal to the expected output");
		
		System.out.println("\n***************************************************\n");
		
		System.out.println("The output of RandomOracle(Hashfunction(\"SHA-512\"), 579) on the seed " + seed9 + " is:");
		System.out.println(GeneralUtil.byteArrayToHexString(ret9));
		if(expectedOutput9.compareToIgnoreCase(GeneralUtil.byteArrayToHexString(ret9)) == 0)
			System.out.println("and it is equal to the expected output");
		else
			System.out.println("and it is not equal to the expected output");
	}
}